<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>C++ Tutorial: Boost</title>
  <meta
 content="C++ Tutorial: Boost"
 name="description" />
  <meta
 content="C++ Tutorial, Boost, mpl, smart_ptr, shared_ptr, lambda, TMP"
 name="keywords" />
  <meta http-equiv="Content-Type"
 content="text/html; charset=ISO-8859-1" />
  <link type="text/css" rel="stylesheet" href="../images/bogostyleWidePre.css" />
</head>
<body>
<div id="page" align="center">
<div id="content" style="width: 800px;">
<div id="logo">
<div class="whitetitle" style="margin-top: 70px;">bogotobogo </div>
</div>
<div id="topheader">
<div class="headerbodytext" align="left"><br />
<strong>Bogotobogo</strong><br />
contact@bogotobogo.com </div>
<div class="smallgraytext" id="toplinks"><a href="../index.html">Home</a>
| <a href="../sitemap.html">Sitemap</a>
| <a href="../blog" target="_blank">Contact Us</a>
</div>
</div>
<div id="menu">
<div class="smallwhitetext" style="padding: 9px;" align="right"><a
 href="../index.html">Home</a>
| <a href="../about_us.html">About Us</a>
| <a href="../products.html">Products</a>
| <a href="../our_services.html">Our Services</a>
| <a href="../blog" target="_blank">Contact Us</a>
</div>
</div>
<div id="submenu">
<div class="smallgraytext" style="padding: 9px;" align="right">
<a href="../gif.html">Gif</a> 
|<a href="../java_applet.html">JavaApplet/Web Start</a>
| <a href="../flash.html">Flash</a>
| <a href="../shockwave.html">ShockWave</a>
| <a href="../svg.html">SVG</a>
| <a href="../iPhone.html">iPhone/iPad</a>
| <a href="../android.html">Android</a>
| <a href="../OnHTML5.html">HTML5</a>
| <a href="../Algorithms/algorithms.html">Algorithms</a>
| <a href="../News/NewsMain.html">News</a>
| <a href="../cplusplus/cpptut.html">C++</a>
| <a href="../Java/tutorial/on_java.html">Java</a>
| <a href="../php/phptut.html">PHP</a>
| <a href="../DesignPatterns/introduction.html">Design Patterns</a>
| <a href="../CSharp/.netframework.html">C#</a>
| <a href="../python/pytut.html">Python</a> 
| <a href="../VisualBasicSQL/introduction.html">Visual Basic</a>
</div>
</div>

<br />
<div id="contenttext">

<!-- Use of this code assumes agreement with the Google Custom Search Terms of Service. -->
<!-- The terms of service are available at http://www.google.com/cse/docs/tos.html -->
<form name="cse" id="searchbox_demo" action="http://www.google.com/cse">
  <input type="hidden" name="cref" value="" />
  <input type="hidden" name="ie" value="utf-8" />
  <input type="hidden" name="hl" value="" />
  <input name="q" type="text" size="40" />
  <input type="submit" name="sa" value="Search" />
</form>
<script type="text/javascript" src="http://www.google.com/cse/tools/onthefly?form=searchbox_demo&lang="></script>

<br />
<div style="padding: 10px;"><span class="titletext">C++ Tutorial - Boost</span></div>

<div id="bookmarkshare">
<script type="text/javascript">var addthis_config = {"data_track_clickback":true};</script>
<a class="addthis_button" href="http://www.addthis.com/bookmark.php?v=250&amp;username=khhong7"><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=khhong7"></script>
</div>

<img src="../images/cplusplus/cpp_logo.jpg" alt="cplusplus logo"/>

<div class="bodytext" style="padding: 12px;" align="justify"> 
<div class="subtitle_2nd" id="FullList">Full List of C++ Tutorials</div>
<ul>
   <li><a href="cpptut.html">C++ Home</a> </li>
   <li><a href="string.html">String</a> </li>
   <li><a href="constructor.html">Constructor</a> </li>
   <li><a href="operatoroverloading.html">Operator Overloading</a> </li>
   <li><a href="virtualfunctions.html">Virtual Functions</a></li>
   <li><a href="dynamic_cast.html">Dynamic Cast Operator</a></li>
   <li><a href="typecast.html">Type Cast Operators</a></li>
   <li><a href="autoptr.html">Class auto_ptr</a></li>
   <li><a href="templates.html">Templates</a></li>
   <li><a href="references.html">References for Built-in Types</a></li>
   <li><a href="valuevsreference.html">Pass by Value vs. Pass by Reference</a></li>
   <li><a href="memoryallocation.html">Memory Allocation</a></li>
   <li><a href="friendclass.html">Friend Functions and Friend Classes</a></li>
   <li><a href="functors.html">Functors (Function Objects)</a></li>
   <li><a href="statics.html">Static Variables and Static Class Members</a></li>
   <li><a href="exceptions.html">Exceptions</a></li>
   <li><a href="pointers.html">Pointers</a></li>
   <li><a href="pointers2.html">Pointers II</a></li>
   <li><a href="pointers3.html">Pointers III</a></li>
   <li><a href="assembly.html">Taste of Assembly</a></li>
   <li><a href="smallprograms.html">Small Programs</a></li>
   <li><a href="linkedlist.html">Linked List Examples</a></li>
   <li><a href="binarytree.html">Binary Tree Example Code</a></li>
   <li><a href="stl.html">Standard Template Library (STL)</a></li>
   <li><a href="stl2.html">Standard Template Library (STL) II - Maps</a></li>
   <li><a href="stl3_iterators.html">Standard Template Library (STL) III - Iterators</a></li>
   <li><a href="slicing.html">Object Slicing and Virtual Table</a></li>
   <li><a href="this_pointer.html">The this Pointer</a></li>
   <li><a href="stackunwinding.html">Stack Unwinding</a></li>
   <li><a href="upcasting_downcasting.html">Upcasting and Downcasting</a></li>
   <li><a href="object_returning.html">Object Returning</a></li>
   <li><a href="private_inheritance.html">Private Inheritance</a></li>
   <li><a href="cplusplus_keywords.html">C++_Keywords</a></li>
   <li><a href="multithreaded.html">Multi-Threaded Programming - Terminology</a></li>
   <li><a href="multithreaded2A.html">Multi-Threaded Programming II - Native Thread for Win32 (A) </a></li>
   <li><a href="multithreaded2B.html">Multi-Threaded Programming II -  Native Thread for Win32 (B) </a></li>
   <li><a href="multithreaded2C.html">Multi-Threaded Programming II -  Native Thread for Win32 (C) </a></li>
   <li><a href="multithreaded2.html">Multi-Threaded Programming II - C++ Thread for Win32</a></li>
   <li><a href="multithreaded3.html">Multi-Threaded Programming III - C++ Class Thread for Pthreads</a></li>
   <li><a href="multithreadedDebugging.html">Multithread Debugging</a></li>
   <li><a href="embeddedSystemsProgramming.html">Embedded Systems Programming</a></li>
   <li><a href="boost.html">Boost</a></li>
   <li>Programming Questions and Solutions
    <ul>
       <li><a href="quiz_strings_arrays.html">Strings and Arrays</a></li>
       <li><a href="quiz_linkedlist.html">Linked List</a></li>
       <li><a href="quiz_recursion.html">Recursion</a></li>
       <li><a href="quiz_bit_manipulation.html">Bit Manipulation</a> </li>
       <li><a href="google_interview_questions.html">140 Google Interview Questions</a> </li>
    </ul>
   </li>

</ul>
</div>
<br />
<img src="images/boost/MoAk1.png" alt="MoAk"/>
<br />
<br />


<div class="bodytext" style="padding: 12px;" align="justify">
<div class="subtitle" id="Introduction">Introduction</div>
<p>Everything we need for introductory materials are at <a href="http://www.boost.org/" target="_blank">http://www.boost.org</a>.
<p>Boost is an organization and it offers portable, peer-reviewed, open source C++ libraries. Most C++ Standards Committee's Library <strong>Technical Report 1 (TR1)</strong> functionality is based on work done at Boost, and until vendors of compilers include TR1 in their C++ library distributions, the <a href="http://www.boost.org/" target="_blank">Boost web site</a> will remain the first stop for developers looking for TR1 implementation.</p>
<p>Boost libraries range from general-purpose libraries like the <strong>smart_ptr</strong> library, 
to operating system abstractions like <strong>Boost FileSystem</strong>, 
to libraries primarily aimed at other library developers and advanced C++ users, 
like the <strong>template metaprogramming (mpl)</strong>.</p> 
<p>In order to ensure efficiency and flexibility, Boost makes extensive use of templates. Boost has been a source of extensive work and research into <strong>generic programming</strong> and <strong>metaprogramming</strong> in C++.</p>
<p>This page will show how we can start to use boost libraries using Windows environment. Since there are other good sites for introductory course, I'm not going into details here.</p>  
<br />
<br />

<div class="subtitle_2nd" id="visualstudio">Using Visual Studio IDE</div>
<p><a href="http://www.boostpro.com/download/" target="_blank">Installer</a> from <a href="http://www.boostpro.com/" target="_blank">BoostPro Computing</a> is recommended, because the installer can download and install precompiled library binaries, saving us the trouble of building them ourselves. </p>
<p>Once installed, let's run our sample code. <br />
Use the following file as our source file, "ex.cpp":</p>
<pre>
#include &lt;boost/lambda/lambda.hpp&gt;
#include &lt;iostream&gt;
#include &lt;iterator&gt;
#include &lt;algorithm&gt;

int main()
{
    typedef std::istream_iterator&lt;int&gt; in;

    std::for_each(
        in(std::cin), in(), std::cout << (boost::lambda::_1 * 10) << " " );
}
</pre>
<p>Then, add the following line to Project Property  > C/C++  > general  > Additional Include Directories:</p>
<pre>
C:\Program Files\boost\boost_1_44
</pre>
<p>Build and Run. Type some numbers. We got it!</p>
<br />
<br />

<div class="subtitle_2nd" id="cmdline">Using Command Line</div>
<p>This time, we're going to do it on command line:</p>
<pre>
All Programs > Microsoft Visual Studio 2005 > <br />
Visual Studio Tools > Visual Studio 2005 Command Prompt
</pre>
<p>Then, run the following, we'll have "ex.exe"</p>
<pre>
cl /EHsc /I whereTo\boost_1_45_0 whereTo\example.cpp
</pre>
<br /><br />

<div class="subtitle" id="TR1">TR1 Components</div>
<p><strong>TR1</strong> specifies new components and all are in the <strong>boost</strong> namespace. For example, the full name of the <strong>shared_ptr</strong> is:</p>
<pre>
boost::shared_ptr
</pre>
<p>Here is the list of the new components:</p>
<br />
<div class="subtitle_2nd" id="smartpointers">smart pointers</div>
<ul>
	<li><strong>shared_ptr</strong><br />
	<p>The <strong>auto_ptr</strong> has unusual characters: copying it whether via copy constructor or copy assignment operator sets it to null, and the copying pointer assumes ownership of the resource as we see in the example below:</p> 
<pre>
#include &lt;iostream&gt;
#include &lt;memory&gt;

using namespace std;
class classA{};

int main()
{
	auto_ptr&lt;classA&gt; ptrA(new classA);

	cout << ptrA.get() << endl;

	auto_ptr&lt;classA&gt; ptrB(ptrA);

	cout << ptrA.get() << endl;
	cout << ptrB.get() << endl;

	return 0;
}
</pre>
<p>Output is:</p>
<pre>
001B0950
00000000
001B0950
</pre>
	<p>Note that the second output is <strong>null</strong>. So, in the copy constructor, <strong>ptrA</strong> transferred the ownership of <strong>classA</strong> object to <strong>ptrB</strong>.</p> 
	<p>This behavior and the underlying requirement that resources managed by <strong>auto_ptr</strong>s must never have more than one <strong>auto_ptr</strong> pointing to them, means that <strong>auto_ptr</strong>s aren't the best way to handle resources which are dynamically allocated. </p>
	<p>So, as an alternative to <strong>auto_ptr</strong>, we have a <strong>referencing-counting smart pointer</strong>. It keeps track of how many objects point to a particular resource and deletes the resource automatically when nothing is pointing to it.</p>
	<p>By replacing <strong>auto_ptr</strong> with <strong>share_ptr</strong>, with an almost same code below, it produces the output we want to:</p>
<pre>
<font color="red">#include &lt;boost/smart_ptr/shared_ptr.hpp&gt;</font>
#include &lt;iostream&gt;
#include &lt;memory&gt;

class classA{};

int main()
{
	<font color="red">boost::shared_ptr</font>&lt;classA&gt; ptrA(new classA);

	std::cout << ptrA.get() << std::endl;

	<font color="red">boost::shared_ptr</font>&lt;classA&gt; ptrB(ptrA);

	std::cout << ptrA.get() << std::endl;
	std::cout << ptrB.get() << std::endl;

	return 0;
}
</pre> 
<p>Output is:</p>
<pre>
002C0950
002C0950
002C0950
</pre>
	<p>Since copying <strong>boost::shared_ptr</strong> works as we expect, it can be used in <strong>STL</strong> containers while we cannot use <strong>std::auto_ptr</strong> </p>
	<p>For dynamically allocated arrays, we shouldn't use either of them because they use <strong>delete</strong> in their destructor but not <strong>delete[]</strong>. We can use <strong>vector</strong> instead. If we insist on using boost, we can use either <strong>boost::shared_array</strong> or <strong>boost::scoped_array</strong>.</p>
	</li>
</ul>

<br />
<div class="subtitle_2nd" id="boostlambda">boost::lambda</div>
<p>There are several libraries which are function objects and higher-order programming. They are used as the 
basis for functionality in TR1. One of those libraries is the <strong>Lambda</strong> library, which makes it 
so easy to create function objects on the fly:<?p>
<pre>
#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;

<font color="red">#include &lt;boost/lambda/lambda.hpp&gt;</font>

using namespace std;

void makeSquare(vector&lt;int&gt;&v)
{
	for_each(v.begin(), v.end(), cout << <font color="red">boost::lambda::_1*boost::lambda::_1</font> << " ");
}

int main()
{
	vector&lt;int&gt; v;
	for(int i = 1; i <= 10; i++) v.push_back(i);

	makeSquare(v);
	return 0;
}
</pre>
<p>Output is:</p>
<pre>
1 4 9 16 25 36 49 64 81 100
</pre>
<p>For each element <strong>x</strong> in <strong>v</strong>, print <strong>x*x</strong>:</p>
<pre>
boost::lambda::_1*boost::lambda::_1
</pre>
<p><strong>_1</strong> is the <strong>Lambda</strong> library's placeholder for the current element.</p>
<br />
<br />


<div class="subtitle_2nd" id="boostmpl">boost::mpl</div>
<p>The boost::mpl is a high-level C++ template <strong>metaprogramming</strong>
 framework of compile-time algorithms, sequences and metafunctions.</p>
<p>Here is an example of calculating a factorial during compilation. It demonstrates looping through 
recursive template instantiation. It also demonstrates one way in which variables are created and used in 
<strong>template metaprogramming (TMP)</strong>:</p>
<pre>
#include &lt;iostream&gt;

template &lt;int n&gt;
struct Factorial 
{
    enum { value = n * Factorial&lt;n - 1&gt;::value };
};
 
template &lt;&gt;
struct Factorial&lt;0&gt; 
{
    enum { value = 1 };
};
 
int main()
{
	std::cout << "Factorial&lt;5&gt;::value = " << Factorial&lt;5&gt;::value << std::endl;	
}
</pre>
<p>The first template is for the general case, and the value of <strong>Factorial&lt;n&gt;</strong> is 
<strong>n</strong> times the value of <strong>Factorial&lt;n-1&gt;</strong>. The second template is for a 
special case, which is a template specialization to provide the ending condition for the recursion, and the value of <strong>Factorial&lt;0&gt;</strong> is <strong>1</strong>.</p>
<p>Note that it calculates factorials at compile time. However, to be able to use templates in this manner, 
the compiler must know the value of its parameters at compile time, which has the natural precondition that <strong>Factorial&lt;n&gt;::value</strong> can only be used if <strong>n</strong> is known at compile time. 
In other words, <strong>n</strong> must be a constant literal or a constant expression.</p>
<p>The looping occurs where the template instantiation <strong>Factorial&lt;n&gt;</strong> references the template 
instantiation <strong>Factorial&lt;n-1&gt;</strong>. Each instantiation of the <strong>Factorial</strong> template is a 
struct, and each struct uses the enum to declare a template metaprogramming variable named <strong>value</strong>. This variable <strong>value</strong> is what holds the current value of the factorial calculation. </p>
<p>Since template metaprogramming uses recursive template instantiation in place of loops, each instantiation gets its 
own copy of <strong>value</strong>, and each copy has the proper value for its place in the loop.</p>



<br />
<br />
<div class="subtitle_2nd" id="boostfunction">boost::function</div>
<br />
<br />
<div class="subtitle_2nd" id="boostbind">boost::bind</div>
<br />
<br />
<div class="subtitle_2nd" id="HashTables">Hash tables</div>
<br />
<br />

<div class="subtitle_2nd" id="RegularExpressions">Regular expressions - boost::regex</div>
<ul>
	<li><strong>E-mail</strong> <br />
<pre>
\b[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,4}\b 
or more general expression
\b[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b
	</li>
	<li><strong>ip address</strong> <br />
<pre>
to match 999.999.999.999
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b
or To restrict all 4 numbers in the IP address to 0..255
\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b
	</li>
</ul>
<div class="subtitle_2nd" id="Tuples">Tuples</div>
<br />
<br />
<div class="subtitle_2nd" id="boostarray">boost::array</div>
<br />
<br />
<div class="subtitle_2nd" id="boostmem_fn">boost::mem_fn</div>
<br />
<br />
<div class="subtitle_2nd" id="boostreference_wrapper">boost::reference_wrapper</div>
<br />
<br />
<div class="subtitle_2nd" id="RandomNumberGeneration">Random number generation</div>
<br />
<br />
<div class="subtitle_2nd" id="specialfunction">Mathematical special functions</div>
<br />
<br />
<div class="subtitle_2nd" id="C99">C99 compatibility extensions</div>
<br />
<br />
<div class="subtitle_2nd" id="TypeTraits">Type traits</div>
<br />
<br />
<div class="subtitle_2nd" id="boostResult_of">boost::result_of</div>






<br /><br />
<img src="images/boost/PahlYoungSan.png" alt="PahlYoungSan"/>
<br />
<br />
<div class="subtitle_2nd" id="FullList">Full List of C++ Tutorials</div>
<ul>
   <li><a href="cpptut.html">C++ Home</a> </li>
   <li><a href="string.html">String</a> </li>
   <li><a href="constructor.html">Constructor</a> </li>
   <li><a href="operatoroverloading.html">Operator Overloading</a> </li>
   <li><a href="virtualfunctions.html">Virtual Functions</a></li>
   <li><a href="dynamic_cast.html">Dynamic Cast Operator</a></li>
   <li><a href="typecast.html">Type Cast Operators</a></li>
   <li><a href="autoptr.html">Class auto_ptr</a></li>
   <li><a href="templates.html">Templates</a></li>
   <li><a href="references.html">References for Built-in Types</a></li>
   <li><a href="valuevsreference.html">Pass by Value vs. Pass by Reference</a></li>
   <li><a href="memoryallocation.html">Memory Allocation</a></li>
   <li><a href="friendclass.html">Friend Functions and Friend Classes</a></li>
   <li><a href="functors.html">Functors (Function Objects)</a></li>
   <li><a href="statics.html">Static Variables and Static Class Members</a></li>
   <li><a href="exceptions.html">Exceptions</a></li>
   <li><a href="pointers.html">Pointers</a></li>
   <li><a href="pointers2.html">Pointers II</a></li>
   <li><a href="pointers3.html">Pointers III</a></li>
   <li><a href="assembly.html">Taste of Assembly</a></li>
   <li><a href="smallprograms.html">Small Programs</a></li>
   <li><a href="linkedlist.html">Linked List Examples</a></li>
   <li><a href="binarytree.html">Binary Tree Example Code</a></li>
   <li><a href="stl.html">Standard Template Library (STL)</a></li>
   <li><a href="stl2.html">Standard Template Library (STL) II - Maps</a></li>
   <li><a href="stl3_iterators.html">Standard Template Library (STL) III - Iterators</a></li>
   <li><a href="slicing.html">Object Slicing and Virtual Table</a></li>
   <li><a href="this_pointer.html">The this Pointer</a></li>
   <li><a href="upcasting_downcasting.html">Upcasting and Downcasting</a></li>
   <li><a href="object_returning.html">Object Returning</a></li>
   <li><a href="private_inheritance.html">Private Inheritance</a></li>
   <li><a href="cplusplus_keywords.html">C++_Keywords</a></li>
   <li><a href="multithreaded.html">Multi-Threaded Programming - Terminology</a></li>
   <li><a href="multithreaded2A.html">Multi-Threaded Programming II - Native Thread for Win32 (A) </a></li>
   <li><a href="multithreaded2B.html">Multi-Threaded Programming II -  Native Thread for Win32 (B) </a></li>
   <li><a href="multithreaded2C.html">Multi-Threaded Programming II -  Native Thread for Win32 (C) </a></li>
   <li><a href="multithreaded2.html">Multi-Threaded Programming II - C++ Thread for Win32</a></li>
   <li><a href="multithreaded3.html">Multi-Threaded Programming III - C++ Class Thread for Pthreads</a></li>
   <li><a href="multithreadedDebugging.html">Multithread Debugging</a></li>
   <li><a href="embeddedSystemsProgramming.html">Embedded Systems Programming</a></li>
   <li><a href="boost.html">Boost</a></li>
   <li>Programming Questions and Solutions
    <ul>
       <li><a href="quiz_strings_arrays.html">Strings and Arrays</a></li>
       <li><a href="quiz_linkedlist.html">Linked List</a></li>
       <li><a href="quiz_recursion.html">Recursion</a></li>
       <li><a href="quiz_bit_manipulation.html">Bit Manipulation</a> </li>
       <li><a href="google_interview_questions.html">140 Google Interview Questions</a> </li>
    </ul>
   </li>
</ul>
<br /><br />




<br />


<br />
<br />
<br />


</div>
</div>
<div class="smallgraytext" id="footer"><a href="../index.html">Home</a>
| <a href="../about_us.html">About Us</a>
| <a href="../products.html">products</a>
| <a href="../our_services.html">Our Services</a>
| <a href="#">Contact Us</a>
| Bogotobogo &copy; 2011 | <a target="_blank" href="http://www.bogotobogo.com">Bogotobogo </a>
</div>
</div>
</div>
</body>
</html>
